package cn.smart.epic.core.support;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther Uncle_Z
 * @date 2017/9/19 22:49
 * @Email:2545375@qq.com
 * @Descrition
 * @since 1.0
 */

public class HttpKit {


    public static String getIp(){
        return HttpKit.getRequest().getRemoteHost();
    }

    /**
     * 获取所有请求的值
     * @return
     */
    public static Map<String,String> getRequestParameters(){
        Map<String, String> values = new HashMap<>();
        HttpServletRequest request=HttpKit.getRequest();
        Enumeration enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String parName = ((String) enumeration.nextElement());
            String parValue = request.getParameter(parName);
            values.put(parName, parValue);
        }
        return values;
    }

    /**
     * 获取 HttpServletRequest
     * @return
     */
    public static HttpServletResponse getResponse(){
        HttpServletResponse response=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getResponse();
        return response;
    }


    /**
     * 获取 包装防Xss Sql注入的 HttpServletRequest
     * @return
     */
    public static HttpServletRequest getRequest(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return new WafRequestWrapper(request);
    }

}
